<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索 -->
      <el-input
        placeholder="请输入内容"
        v-model="searchNewVul"
        clearable
        @input="recoverList"
        @keyup.enter.native="searchFn"
        class="input-with-select"
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchFn"
        ></el-button>
      </el-input>
      <!-- 商品表格 -->
      <el-table :data="goodsList" stripe border style="width: 100%">
        <!-- 序号 -->
        <el-table-column type="index" :index="indexMethod" width="50">
        </el-table-column>
        <!-- 商品名称 -->
        <el-table-column prop="goods_name" label="商品名称" width="950">
        </el-table-column>
        <!-- 商品价格 -->
        <el-table-column prop="goods_price" label="商品价格" width="120">
        </el-table-column>
        <!-- 商品重量 -->
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <!-- 创建时间 -->
        <el-table-column label="创建时间" width="180">
          <template slot-scope="scope">{{
            scope.row.add_time | dateFormat
          }}</template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.goods_id)"
              icon="el-icon-delete"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[5, 10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper, ->"
        background
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchNewVul: '',
      searchOldVul: '',
      currentPage: 1,
      goodsList: [],
      total: 0,
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5,
      },
      flag: false,
    }
  },
  created() {
    this.queryInfo.query = ''
    this.getGoodsList()
  },
  methods: {
    // 获取数据
    async getGoodsList() {
      const res = await this.$http({
        url: '/goods',
        params: this.queryInfo,
      })
      console.log('商品列表', res)
      this.total = res.data.data.total
      this.goodsList = res.data.data.goods
    },
    // 渲染表格
    async renderGoodsList() {
      this.queryInfo.query = this.searchNewVul
      this.getGoodsList()
    },
    // 删除
    handleDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$http.delete(`goods/${id}`).then((res) => {
            if (res.data.meta.status !== 200) {
              this.$message.error('删除失败！')
            } else {
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              this.reSearch()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    indexMethod(index) {
      return index + 1
    },
    // 切换每页条数
    async handleSizeChange(val) {
      this.queryInfo.pagesize = val
      console.log(`每页 ${val} 条`)
      this.reSearch()
    },
    // 跳页
    async handleCurrentChange(val) {
      this.queryInfo.pagenum = val
      console.log(`当前页: ${val}`)
      this.reSearch()
    },
    // 搜索
    async searchFn() {
      if (this.searchNewVul) {
        this.queryInfo.pagenum = 1
        this.queryInfo.query = this.searchNewVul
        await this.getGoodsList()
        if (this.total > 0) {
          this.flag = true
          this.searchOldVul = this.searchNewVul.trim()
          this.currentPage = 1
          this.$message({
            message: '获取商品列表成功',
            type: 'success',
          })
        } else {
          this.$message.error('没有该商品')
        }
      }
    },
    // 当搜索框没字时，初始化商品列表
    recoverList() {
      if (!this.searchNewVul) {
        this.flag = false
        this.renderGoodsList()
      }
      this.flag = false
    },
    // 判断用户是否再次点击搜索
    async reSearch() {
      if (this.flag) {
        this.queryInfo.query = this.searchOldVul
        this.getGoodsList()
      } else {
        this.queryInfo.query = ''
        this.getGoodsList()
      }
    },
  },
}
</script>

<style lang="less" scoped>
.el-input {
  width: 500px;
}
</style>